<template>
  <div class="box">
    <!-- 导航 -->
    <div class="title">
      <div class="title_fan row-wrapper">
        <span class="el-icon-arrow-left" @click="$router.go(-1)"></span>
        <span>我的收款二维码</span>
        <span></span>
      </div>
      <div class="title_back_img">
        <div class="mini_back">
          <div>
            <img v-show="$route.query.type==='pay'"
                 src="../../../../assets/static/payQrcode/pay.png" alt="">
          </div>
          <div>钱包余额</div>
          <div>
            <img src="../../../../assets/static/membership/a4.png" alt="">
            <span>{{ balacn }}</span>
          </div>
        </div>
        <div class="big_back">
          <img :src="qrcode" alt="">
          <div>
            <div class="progress">
              <div :style="progress" class="back_progress"></div>
            </div>
            <div>二维码刷新时间剩余{{ flushTime }}s</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {paytoken, paycode} from '@/api/merbership/pay'
import {getUid} from '@/utils/tokenTools'
import {searchorder} from '@/api/merbership/userpay'
import {balance} from '@/api/merbership/user'
import '@/styles/flax.css'
export default {
  data() {
    return {
      payToken: '',
      qrcode: '',
      flushTime: 30,
      order_id: 1,
      orderInterval: null,
      interval: null,
      balacn: '0.00'
    }
  },
  computed: {
    progress() {
      if (this.flushTime === 30 || this.flushTime === 0) {
        return {
          'width': String(this.flushTime / 30 * 100) + '%',
          'transition': 'width 0s'
        }
      } else {
        return {
          'width': String(this.flushTime / 30 * 100) + '%',
          'transition': 'width 2s'
        }
      }
    }
  },
  mounted() {
    this.paytoken()
    this.countDown()
    this.balance()
    // this.searchorderInterval()
  },
  beforeDestroy() {
    clearInterval(this.interval)
  },
  methods: {
    /**
     * 定时轮询订单是否支付成功
     */
    searchorderInterval() {
      this.orderInterval = setInterval(() => {
        this.searchorder()
      }, 3000)
    },
    /**
     * 二维码刷新倒计时
     */
    countDown() {
      let that = this
      this.interval = setInterval(() => {
        that.flushTime -= 1
      }, 1000)
    },
    // -------------------- axios ------------------------
    /**
     * 用户余额
     */
    balance() {
      const pd = {}
      balance(this.$qs.stringify(pd))
        .then(res => {
          this.balacn = res.data.data.balance
        })
    },
    /**
     * 查询订单是否支付成功
     */
    searchorder() {
      const pd = {
        order_id: this.order_id
      }
      searchorder(this.$qs.stringify(pd))
        .then(res => {
          this.$message.info(res.data.msg)
        })
    },
    /**
     *
     * 支付二维码
     */
    paycode() {
      const pd = {
        token: this.payToken
      }
      paycode(this.$qs.stringify(pd))
        .then(res => {
          this.qrcode = res.data.data
        })
    },
    /**
     * 支付参数pay token
     */
    paytoken() {
      const pd = {
        order_id: getUid()
      }
      paytoken(this.$qs.stringify(pd))
        .then(res => {
          this.payToken = res.data.data
          this.paycode()
        })
    }
  },
  beforeDistory() {
    clearInterval(this.orderInterval)
  },
  watch: {
    flushTime(val) {
      if (val === 0) {
        clearInterval(this.interval)
        this.flushTime = 30
        this.countDown()
        this.paytoken()
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.box {
  .title {
    background-image: linear-gradient(to bottom, #7AC91B 25%, #f5f5f5 0);

    .title_fan {
      justify-content: space-between;
      color: #fff;
      font-size: 18px;
      padding: 15px;
    }
  }

  .title_back_img {
    background-image: url(../../../../assets/static/membership/a5.png);
    background-repeat: no-repeat;
    background-size: 100% 100%;
    padding: 5px 0;

    .mini_back {
      text-align: center;
      font-size: 15px;
      padding: 20px 0;

      > div:nth-child(1) {
        img {
          width: 45px;
        }
      }

      > div:nth-child(3) {
        display: flex;
        justify-content: center;
        align-items: center;
        margin-top: 15px;

        img {
          width: 25px;
          height: 25px;
        }

        span {
          font-size: 28px;
          font-weight: 600;
          margin-left: 13px;
        }
      }
    }

    .big_back {
      margin: 50px;
      text-align: center;
      font-size: 11px;

      img {
        width: 90%;
      }

      .progress {
        margin: 5px 0;
        width: 100%;
        height: 10px;
        padding: 5px 4px;
        background-image: url(../../../../assets/static/membership/a2.png);
        background-size: 100% 100%;
        background-repeat: no-repeat;

        .back_progress {
          background-color: #FFB200;
          //width: 30%;
          height: 10px;
          border-radius: 15px;
        }
      }
    }
  }
}
</style>
